var app = getApp();
var SERVER_URL=app.globalData.SERVER_URL;

Page({
    data: {
    },
    onLoad(){
        this.getType();
        this.setData({

        });
    },
    onShow:function(){
        this.setData({

        });
    },
    form_submit:function(){
        var data = this.data;
        var content = data.content ||"";
        var address = data.address ||"";
        var logs=data.logs ||"";

        if(logs == ""){
            wx.showModal({
                title: '失败',
                content: '工作日志不能为空',
                showCancel:false,
                success: res=>{
                    if (res.confirm) {

                    }
                }
            });
            return;
        }

        //submit
        var openId = wx.getStorageSync('openId');
        var url="/work/addWork";
        url = SERVER_URL+url;
        wx.request({
            url: url,
            data: {
                "key": app.globalData.key,
                "openId": openId,
                "dailyRecord":logs,
            },
            method: 'POST',
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success:function(res){
                let result = res.data;
                console.log(result);
                if(result && result.code==0){
                    app.globalData.data.carTarget="";
                    wx.showModal({
                        title: '成功',
                        content: '已成功提交申请',
                        showCancel:false,
                        success: res=>{
                            if (res.confirm) {
                                wx.navigateBack({
                                    delta: 1
                                });
                            }
                        }
                    });
                }
                else{
                    wx.showModal({
                        title: '失败',
                        content: result.msg,
                        showCancel:false,
                        success: res=>{
                            if (res.confirm) {

                            }
                        }
                    });
                }
            }
        })
    },
    text_change:function(e){
        var index = e.currentTarget.dataset.text;
        index = parseInt(index);
        var value = e.detail.value;
        switch (index){
            //内容
            case 1:
                this.setData({
                    content:value
                });
                break;
            //工作地点
            case 2:
                this.setData({
                    address:value
                });
                break;
            //工作日志
            case 3:
                this.setData({
                    logs:value
                });
                break;
        }
    },
    getType:function(){
        let url = "/work/newWork";
        let openId = wx.getStorageSync('openId');
        url = SERVER_URL+url;
        var obj=[];
        var _self=this;
        wx.request({
            url: url,
            data: {
                "key": app.globalData.key,
                openId: openId,
            },
            method: 'GET',
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success:function(res){
                let result = res.data;
                if(result && result.code==0){
                    _self.setData({
                        work:result.data[0],
                    });
                }
            }
        });
    },
});